package ontology;

import java.util.List;
import java.util.ArrayList;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * This class creates an ontology object by parsing both the 
 * frames (frXML/frames.xml) and relations (frXML/reRelation.xml) 
 * files in the FrameNet ontology. The frames file has the following 
 * format:
 * 
 * frames
 *   > frame*
 *   
 * The relations file has the following format:
 * 
 * fr-relations
 *   > frame-relation-type*
 * 
 * One of the frame relation types is called "CoreSet" and this is
 * singled out for special treatment.   
 */

public class Ontology {

	public List<Frame> frames = new ArrayList<Frame>();
	public List<FrameRelationType> relations = new ArrayList<FrameRelationType>();
	public List<CoreSet> coresets = new ArrayList<CoreSet>();

	public Ontology(Element e,Element f) {
		NodeList ns = e.getChildNodes();
		for (int i=0;i<ns.getLength();i++) {
			Node n = ns.item(i);
			String s = n.getNodeName();
			if (s.equals("frame")) {
				frames.add(new Frame((Element) n));
			}
		}
		ns = f.getChildNodes();
		for (int i=0;i<ns.getLength();i++) {
			Node n = ns.item(i);
			String s = n.getNodeName();
			if (s.equals("frame-relation-type")) {
				relations.add(new FrameRelationType((Element) n));
			}
		}
		// factor out the coreness sets:
		for (FrameRelationType r: relations) {
			if (r.name.equals("CoreSet")) {
				for (FrameRelation l: r.links) {
					for (FERelation a: l.feLinks) {
						// every links between FEs becomes a separate coreness set 
						coresets.add(new CoreSet(l,a));
					}
				}
				break;
			}
		}		
		// consolidate coreness sets:
		List<CoreSet> coresetsOld = coresets;
		coresets = new ArrayList<CoreSet>();
		for (CoreSet c: coresetsOld) {
			boolean found = false;
			for (CoreSet d: coresets) {
				if (c.frame.equals(d.frame) && c.name.equals(d.name)) {
					found = true;
					break;
				}
			}
			if (!found) {
				coresets.add(c);
			}
		}
	}
	
}
